package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AlertDataServiceImpl implements AlertDataService {


    @Resource
    private AlertDataMapper alertDataMapper;

    @Override
    public PageResponse<AlertDataVo> selectByPage(AlertDataPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        //转换时间的类型
        Long startTime = dto.getStartTime();
        Long endTime = dto.getEndTime();

        dto.setStartDate(ObjectUtil.isEmpty(startTime) ? null: LocalDateTimeUtil.of(startTime));
        dto.setEndDate(ObjectUtil.isEmpty(endTime) ? null: LocalDateTimeUtil.of(endTime));

        Page<AlertDataVo> page = alertDataMapper.selectByPage(dto);
        return PageResponse.of(page,AlertDataVo.class);

    }

    /**
     * 处理预警数据
     * @param id
     * @param dto
     * @return
     */
    public void handleAlertData(Long id, AlertDataHandleDto dto) {

        AlertData alertData = alertDataMapper.selectById(id);

        alertData.setProcessingResult(dto.getProcessingResult());
        alertData.setProcessingTime(dto.getProcessingTime());

        //通过ThreadLocal获取当前操作用户
        Long userId = UserThreadLocal.getUserId();

        alertData.setProcessorId(userId);
        alertData.setStatus(1);

        //储存新数据到数据库
         alertDataMapper.update(alertData);
    }
}
